﻿#include <QPushButton>
#include <QProgressBar>
#include <QLabel>

#include <QVBoxLayout>
#include <QHBoxLayout>

#include <QDebug>

#include "yuvvideoplaywidget.h"
#include "yuvdispalywidget.h"
#include "yuvmsgnotifysington.h"

YUVVideoPlayWidget::YUVVideoPlayWidget(QWidget *parent)
    : YUVWidget(parent)
{
    setObjectName("YUVVideoPlayWidget");
    setStyleSheet(QLatin1String(Utils::FileReader::fetchQrc(":/resource/zh_ch/qss/videoPlay.css")));

    m_bIsPlaying = false;

    m_pDisplayWidget = new YUVDispalyWidget(this);
    m_pDisplayWidget->setMinimumSize(400, 300);

    m_pProgressBar = new QProgressBar(this);
    m_pProgressBar->setTextVisible(true);
    m_pProgressBar->setValue(0);

    m_pPreFrameButton = new QPushButton(tr("prevFrame"), this);
    m_pNextFrameButton = new QPushButton(tr("nextFrame"), this);
    m_pPlayButton = new QPushButton(tr("play"), this);
    m_pStopButton = new QPushButton(tr("stop"), this);
    m_pShowRateLabel = new QLabel("0/0", this);

    m_pPreFrameButton->setToolTip(tr("prevFrame"));
    m_pNextFrameButton->setToolTip(tr("nextFrame"));
    m_pPlayButton->setToolTip(tr("play"));
    m_pStopButton->setToolTip(tr("stop"));

    m_pPreFrameButton->setCheckable(true);
    m_pNextFrameButton->setCheckable(true);
    m_pPlayButton->setCheckable(true);
    m_pStopButton->setCheckable(true);

    connect(m_pPreFrameButton, SIGNAL(clicked(bool)), this, SLOT(enterPreFrame()));
    connect(m_pNextFrameButton, SIGNAL(clicked(bool)), this, SLOT(enterNextFrame()));
    connect(m_pPlayButton, SIGNAL(clicked(bool)), this, SLOT(startPlay()));
    connect(m_pStopButton, SIGNAL(clicked(bool)), this, SLOT(stopPlay()));
    connect(m_pDisplayWidget, SIGNAL(sigOnFrame(quint64,quint64)), this, SLOT(setProgressValue(quint64,quint64)));

    QHBoxLayout *pControlLayout = new QHBoxLayout;
    pControlLayout->addWidget(m_pShowRateLabel);
    pControlLayout->addStretch();
    pControlLayout->addWidget(m_pStopButton);
    pControlLayout->addWidget(m_pPreFrameButton);
    pControlLayout->addWidget(m_pPlayButton);
    pControlLayout->addWidget(m_pNextFrameButton);
    pControlLayout->addStretch();

    QVBoxLayout *pMainLayout = new QVBoxLayout;
    pMainLayout->addWidget(m_pDisplayWidget);
    pMainLayout->addWidget(m_pProgressBar);
    pMainLayout->addLayout(pControlLayout);

    setLayout(pMainLayout);

    connect(YUVMsgNotifySington::instance(), SIGNAL(playInit(QString)),
            this, SLOT(playFileInit(QString)));

    m_pBtnPrev = m_pPlayButton;
}

YUVVideoPlayWidget::~YUVVideoPlayWidget()
{

}

void YUVVideoPlayWidget::resetFilePath(QString filePath)
{
    if (!m_bIsPlaying && (m_filePath != filePath))
    {
        m_filePath = filePath;
        m_pDisplayWidget->init(filePath);
    }
}

void YUVVideoPlayWidget::enterPreFrame()
{
    changeCheckedButton(m_pPreFrameButton);
    if (m_bIsPlaying)
    {
        startPlay();
    }

    m_pDisplayWidget->prevFrame();
}

void YUVVideoPlayWidget::enterNextFrame()
{
    changeCheckedButton(m_pNextFrameButton);
    if (m_bIsPlaying)
    {
        startPlay();
    }

    m_pDisplayWidget->nextFrame();
}

void YUVVideoPlayWidget::startPlay()
{
    if (m_filePath.isEmpty())
    {
        return;
    }

    changeCheckedButton(m_pPlayButton);
    if (!m_bIsPlaying)
    {
        m_pPlayButton->setText(tr("pause"));
        m_pDisplayWidget->play();
    }
    else
    {
        m_pPlayButton->setText(tr("play"));
        m_pDisplayWidget->pause();
    }

    m_bIsPlaying = !m_bIsPlaying;
}

void YUVVideoPlayWidget::stopPlay()
{
    changeCheckedButton(m_pStopButton);
    if (m_bIsPlaying)
    {
        m_pPlayButton->setText(tr("play"));
        m_pDisplayWidget->stop();
        m_bIsPlaying = !m_bIsPlaying;
    }
}

void YUVVideoPlayWidget::playFileInit(QString filePath)
{
    stopPlay();

    if (m_filePath != filePath) {
        m_filePath = filePath;
        m_pDisplayWidget->init(filePath);
    }

    startPlay();
}

void YUVVideoPlayWidget::setProgressValue(quint64 nCurFrameIndex, quint64 nTotalFrames)
{
    m_pProgressBar->setMaximum(nTotalFrames);
    m_pProgressBar->setValue(nCurFrameIndex);

    m_pShowRateLabel->setText(QString("%1/%2").arg(nCurFrameIndex).arg(nTotalFrames));

    //播放完毕，停止播放
    if (nCurFrameIndex == nTotalFrames) {
        stopPlay();
    }
}

void YUVVideoPlayWidget::changeCheckedButton(QPushButton *pBtn)
{
    m_pBtnPrev->setChecked(false);
    pBtn->setChecked(true);
    m_pBtnPrev = pBtn;
}
